package com.whty.cms.filewps.util;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import java.net.Inet4Address;
import java.net.UnknownHostException;

public class SnowFlakeUtil {
	
	private static SnowFlake instance;

    public static long getNextId() {
        if (instance == null) {
            /*String ipAddr = null;
            List<String> ipAddrList = IpUtil.listLocalIp4();
            if (ipAddrList != null && ipAddrList.size() > 0) {
                ipAddr = ipAddrList.get(0);
            }*/
            /*long datacenterId = 0L;
            long workerId = 0L;
            Map<String,Object> snowflake = getSnowflake(ipAddr);
            if (snowflake != null && !snowflake.isEmpty()) {
                workerId = snowflake.get("WORKER_ID") != null ? Long.valueOf(snowflake.get("WORKER_ID").toString()) : 0L;
                datacenterId = snowflake.get("DATACENTER_ID") != null ? Long.valueOf(snowflake.get("DATACENTER_ID").toString()) : 0L;
            }*/
        	Long workerId = getWorkId();
        	Long datacenterId = getDataCenterId();
        	System.out.println(workerId+"=============="+datacenterId);
            instance = new SnowFlake(workerId, datacenterId);
//            instance = new SnowFlake(new Random().nextInt(30), new Random().nextInt(30));
        }
        return instance.nextId();
    }

    /*private static Map<String,Object> getSnowflake() {
        if (StringUtils.isBlank(ipAddr)) {
            return null;
        }
        String key = "snowflake_" + ipAddr;
        Map<String,Object> resultMap = (Map<String,Object>) MemcacheUtil.getObj(key);
        if (resultMap == null || resultMap.isEmpty()) {
        	resultMap.put("worker_id", resultMap.get("work_id"));
        	resultMap.put("datacenter_id", resultMap.get("datacenter_id"));
            resultMap = DBUtil.queryForMap("select worker_id,datacenter_id from t_snowflake where ip_addr=?", new Object[]{ipAddr});
        	resultMap = MapUtil.newHashMap();
        	resultMap.put("worker_id", resultMap.get("work_id"));
        	resultMap.put("datacenter_id", resultMap.get("datacenter_id"));
        	if (!resultMap.isEmpty()) {
                MemcacheUtil.putObj(key, resultMap, 2 * 60 * 60); // 缓存两小时
            }
        }
        return resultMap;
    }*/
    
    private static Long getDataCenterId(){
    	try {
	        int[] ints = StringUtils.toCodePoints(SystemUtils.getHostName());
	        int sums = 0;
	        for (int i: ints) {
	            sums += i;
	        }
	        return (long)(sums % 32);
    	} catch (Exception e) {
            // 如果获取失败，则使用随机数备用
            return RandomUtils.nextLong(0,31);
        }
    }
	
	private static Long getWorkId(){
        try {
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            int[] ints = StringUtils.toCodePoints(hostAddress);
            int sums = 0;
            for(int b : ints){
                sums += b;
            }
            return (long)(sums % 32);
        } catch (UnknownHostException e) {
            // 如果获取失败，则使用随机数备用
            return RandomUtils.nextLong(0,31);
        }
    }

}
